<?php
	require_once("db_connection.php");
	
	$search = $_POST['search'];
	
	$data = array();
	
	
	if(mysql_num_rows($result=mysql_query("SELECT * FROM country WHERE countryname='$search'"))){
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		$data[] = array(
			0 => "COUNTRY",
			1 => $row['countryid'],
			2 => $row['continentname'],
			2 => $row['countryname'],
			3 => $row['countrycode'],
			4 => "|"
		);
		$result = mysql_query("SELECT * FROM city WHERE countryid='$row[countryid]'");
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$data[] = array(
				0 => $row['cityid'],
				1 => $row['cityname'],
				2 => "|"
			);
		}
	}else if(mysql_num_rows($result=mysql_query("SELECT * FROM city WHERE cityname='$search'"))){
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		$data[] = array(
			0 => "CITY",
			1 => $row['cityid'],
			2 => $row['cityname'],
			3 => $row['countryid'],
			4 => $row['zipcode'],
			5 => "|"
		);
		$result = mysql_query("SELECT * FROM affiliate WHERE cityid='$row[cityid]' ORDER by priority");
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$data[] = array(
				0 => $row['affiliateid'],
				1 => $row['companyname'] ,
				2 => $row['priority'] ,
				3 => "|"
			);
		}
	}else if(mysql_num_rows($result=mysql_query("SELECT * FROM affiliate WHERE companyname='$search'"))){
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$data[] = array(
				0 => "AFFILIATE",
				1 => $row['companyname'],
				2 => $row['companyaddress'],
				3 => $row['companyemail'],
				4 => $row['landline'],
				5 => $row['faxno'],
				6 => $row['mobileno1'],
				7 => $row['cityid'],
				8 => $row['websiteurl'],
				9 => $row['contactname'],
				10 => $row['contactemail'],
				11 => $row['affiliateid']
			);
		}
	}else if(mysql_num_rows($result=mysql_query("SELECT * FROM drivers WHERE driversname='$search'"))){
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$data[] = array(
				0 => "DRIVER",
				1 => $row['affiliateid'],
				2 => $row['driversname'],
				3 => $row['contactno'],
				4 => $row['image']
			);
		}
	}else if(mysql_num_rows($result=mysql_query("SELECT * FROM vehicles WHERE vehicletype='$search' or vehiclemodel='$search'"))){
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
			$data[] = array(
				0 => "VEHICLE",
				1 => $row['vehicletype'],
				2 => $row['capacity'],
				3 => $row['vehiclemodel'],
				4 => $row['image'],
				5 => "|"
			);
		}
	}
	echo json_encode($data);
	flush();
?>